package clientserver;

import java.awt.Desktop;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * Class to send file from client to server and from server to client
 * @author Maxim Rahman
 *
 */
public class AttachedFile  implements Serializable{

	
	private static final long serialVersionUID = -2507554790014157939L;

	private String  filename;  //  filename
	private String  fullpathonclient; // full path to file on client computer
	private int  requestid; // to which request this file attached
	private boolean  serverside;  //  true  when obect is on server side

	public  byte[]  data;
    private int rid; 
	private static  final String save_path = "C:\\ICM\\";  //save path for files on server HD:
	private static  final String temp_path = "C:\\ICM_temp\\";
				// full path to file on server will be : save_path+"\\"rid+"\\"+filename

	
	//getters and setters//////////////////////////
	public void onserverside()
	{
		serverside=true;
	}
	public void clientside()
	{
		serverside=false;
	}
	
	public String getFilename() {
		return filename;
	}
	public void setFilename(String fn)
	{
		filename=fn;
		
	}

	

	public int getRid() {
		return requestid;
	}

	public void setRid(int requestid) {
		this.requestid = requestid;
	}
	
	
	
	/*runs on client side
	 *   set file position on client computer
	 */
	public void setonclient( String dir,String fn) {
		filename=fn;
		
		String fpath=dir+"\\"+fn;
	fullpathonclient=fpath;

	}
	
///////////////////////////////////////////////////
/*
 * serverorclientside =true if we  create this object on server sider, otherwise false
 *  	
 */
public AttachedFile( boolean serverorclientside) {


	serverside=serverorclientside;
File d = new File(save_path);
if(d.exists()==false&&serverside==true)  // create save path on Server on first running on server
	d.mkdir();

}



/* Runs on client's or server side load file into object
 * Try's to read file from client's computer( serverside==false) or server computer (serverside==true)
 *  into the object, could throw IOException
 * 
 */
public void loadfile() throws IOException
{
	String realpath;
	if(serverside) realpath=save_path+requestid+"\\"+filename;
	else realpath=fullpathonclient;
	 File lfile= new File(realpath);

	 InputStream bis = new BufferedInputStream(new FileInputStream(lfile));
	 
	 int   datalength= (int) lfile.length();
	data = new byte[(int)datalength];


bis.read(data);	

 bis.close();
}

/*returns fgull path of  saved temp file on client computer
 * 
 */
 public String gettempfullpath()
 {
	return temp_path+"\\"+filename; 
 }





/*Runs on server's or client's side
 *  trying to save a file into server if file not exist and all ok returns true
 *  if runs on client side save file into temp directory on client for future usage
 *  otherwise false
 */
public boolean savefile() throws IOException
{
	String savepath;
	if(serverside)
 savepath=save_path+requestid+"\\";  // for save into server
	else
		savepath=temp_path+"\\";  // save into client
	String fullpath=savepath+filename;

 File  dir=new File(savepath);
 dir.mkdirs();
	
	File sfile= new File(fullpath); 

	BufferedOutputStream	bos = new BufferedOutputStream(new FileOutputStream(sfile));
	sfile.createNewFile();

bos.write(data, 0, data.length);
bos.flush();
bos.close();

return true;
}





public static void main(String[] args) throws IOException, InterruptedException 
{
	  String  host = "localhost";
	  int port = 1234;  //The port number


	  Client cl= new Client(host,port);

	   /*  How to send files from client to server example********************/
AttachedFile f =new AttachedFile(false);
String dir="c:\\";
String ff= "123.txt";
f.setonclient(dir, ff);


	try {
		f.loadfile();
	
	f.setRid(12);
	try {
		cl.SendFile(f);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}


catch (IOException e) {
		e.printStackTrace();
	}
	
	AttachedFile fe =new AttachedFile(false);
	 dir="c:\\";
	 ff= "111.txt";
	fe.setonclient(dir, ff);

		try {
			fe.loadfile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	fe.setRid(12);

		cl.SendFile(fe);

	
	
	
	
	/*
	*********************how to get file from server and show him in windows ******************************/
	/*
	AttachedFile  f= new AttachedFile(false);
	f.setRid(12);
	String ff= "1.pdf";
	f.setFilename(ff);
	
	try {
		AttachedFile fl= cl.GetFile(f);
		
fl.clientside();
fl.savefile();
File file= new File(fl.gettempfullpath());
	Desktop.getDesktop().open( file );	
	}

	
	catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} */
System.out.println("done.");




	

}













	
}



